layui.use(['layer', 'table', 'HttpRequest', 'tree','func'], function () {
    var $ = layui.jquery;
    var HttpRequest = layui.HttpRequest;
    var table = layui.table;
    var tree = layui.tree;
    var func = layui.func;

    new HttpRequest(Feng.ctxPath + "/teacher/getConfig?configId=1000", 'get', function (data) {
        if(data.data.configValue==='false'){
            $("#btnStart").show();
            $("#btnEnd").hide();
        }else{
            $("#btnStart").hide();
            $("#btnEnd").show();
        }
        $("#btnStart").click(function(){
            let obj={"configId":"1000","configValue":"true"};
            $.ajax({
                type: 'post',
                url: Feng.ctxPath + "/teacher/editconfig",
                contentType: "application/json",
                dataType: "json",
                async: false,
                data: JSON.stringify(obj),
                success: function (data) {
                    $("#btnStart").hide();
                    $("#btnEnd").show();
                }
            });
        });
        $("#btnEnd").click(function(){
            let obj={"configId":"1000","configValue":"false"};
            $.ajax({
                type: 'post',
                url: Feng.ctxPath + "/teacher/editconfig",
                contentType: "application/json",
                dataType: "json",
                async: false,
                data: JSON.stringify(obj),
                success: function (data) {
                    $("#btnStart").show();
                    $("#btnEnd").hide();
                }
            });
        });

    }, function (data) {

    }).start();

    let TeacherTable={
        id: "TeacherTable",
        lefttree:null,
        renderQuery:function(obj){
            let query = {};
            if(obj.data.id.toString().length==12){
                query['areaCode']=selObj.data.id;
                query['orgId']='';
            }else{
                query['areaCode']='';
                query['orgId']=selObj.data.id;
            }
            return query;
        },
        reload:function(queryData){
            table.reload(TeacherTable.id, {
                where: queryData,
                page: {curr: 1}
            });
        },
        // 重置密码
        resetPassword : function (data) {
            Feng.confirm("是否重置密码为123456?", function () {
                var httpRequest = new HttpRequest(Feng.ctxPath + "/teacher/resetPwd", 'post', function (data) {
                    Feng.success("重置密码成功!");
                    TeacherTable.reload(TeacherTable.renderQuery(selObj));
                }, function (data) {
                    Feng.error("重置密码失败!" + data.message + "!");
                });
                httpRequest.set(data);
                httpRequest.start(true);
            });
        },
        onDeleteUser : function (data) {
            var operation = function () {
                var httpRequest = new HttpRequest(Feng.ctxPath + "/sysUser/delete",'post', function (data) {
                    Feng.success("删除成功!");
                    TeacherTable.reload(TeacherTable.renderQuery(selObj));
                }, function (data) {
                    Feng.error("删除失败!" + data.message + "!");
                });
                httpRequest.set(data);
                httpRequest.start(true);
            };
            Feng.confirm("是否删除用户" + data.account + "?", operation);
        },
        roleAssign : function (data) {
            func.open({
                title: '分配角色',
                content: Feng.ctxPath + '/view/teacher/roleView?userId=' + data.userId,
                tableId: TeacherTable.tableId,
                endCallback: function () {
                    TeacherTable.reload(TeacherTable.renderQuery(selObj));
                }
            });
        },
        openEditDlg : function (data) {
            func.open({
                title: '修改用户',
                content: Feng.ctxPath + '/view/teacher/edit?userId=' + data.userId,
                tableId: TeacherTable.tableId,
                endCallback: function () {
                    TeacherTable.reload(TeacherTable.renderQuery(selObj));
                }
            });
        }
    };

    /* 渲染表格 */
    table.render({
        url: Feng.ctxPath + '/teacher/page',
        elem: '#'+TeacherTable.id,
        data: [],
        height: 'full-100',
        page: true,
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        //toolbar: '#organizationUserTbToolBar',
        cellMinWidth: 80,
        cols: [[
            {type: 'numbers'},
            {field: 'userId', hide: true, sort: true, title: '用户id'},
            // {field: 'realName', align: "center", sort: true, title: '姓名'},
            {field: 'account', align: "left", sort: true, title: '账号'},
            // {field: 'birthday', align: "left", sort: true, title: '出生日期',templet: function (data) {
            //         return layui.util.toDateString(data.birthday, "yyyy-MM-dd");
            // }},
            // {field: 'sex', align: "center", sort: true, title: '性别', width: 95,templet: function (data) {
            //         if (data.sex === 'M') {
            //             return '<span class="layui-badge layui-badge-blue">男</span>';
            //         } else {
            //             return '<span class="layui-badge layui-badge-red">女</span>';
            //         }
            //     }
            // },
            // {field: 'phone', align: "left", sort: false, title: '手机'},
            {field: 'orgName', align: "left", sort: true, title: '所属幼儿园', minWidth: 120},
            {align: 'center', toolbar: '#teacherTableBar', title: '操作', width: 265}

        ]],
        done: function () {
        },
        parseData: Feng.parseData
        ,autoSort: false
    });

    /* 表格工具条点击事件 */
    table.on('tool(TeacherTable)', function (obj) {
        if (obj.event === 'edit') { // 修改
            TeacherTable.openEditDlg(obj.data);
        } else if (obj.event === 'delete') { // 删除
            TeacherTable.onDeleteUser(obj.data);
        } else if (obj.event === 'roleAssign') {
            TeacherTable.roleAssign(obj.data);
        } else if (obj.event === 'reset') {
            TeacherTable.resetPassword(obj.data);
        }
    });

    table.on('sort(TeacherTable)', function (obj) {
        table.reload(TeacherTable.id, {
            initSort: obj //记录初始排序
            , where: {
                sortBy: obj.field //排序字段
                , orderBy: obj.type //排序方式
            }
        });
    });
    $("#btnSearch").click(function(){
        let queryData=TeacherTable.renderQuery(selObj);
        queryData['account'] = $("#account").val();
        TeacherTable.reload(queryData);
    })

    $('#btnAdd').click(function () {
        if ("1339554696976781408" == selObj.data.id) {
            Feng.error("请选择一个幼儿园!");
            return ;
        }
        if (selObj.data.id.toString().length==12) {
            Feng.error("请选择一个幼儿园!");
            return ;
        }
        func.open({
            title: '添加账号',
            content: Feng.ctxPath + '/view/teacher/add?orgId='+selObj.data.id,
            tableId: TeacherTable.tableId,
            endCallback: function () {
                renderTree();
            }
        });
    });

    function renderTree() {
        // 初始化组织树
        new HttpRequest(Feng.ctxPath + "/kindergarten/tree", 'get', function (res) {
            TeacherTable.lefttree = tree.render({
                elem: '#organizationTree',
                onlyIconControl: true,
                data: res.data,
                click: function (obj) {
                    selObj = obj;
                    /*if(obj.data.id=='120112000000') {
                        $('#organizationTree').find('.layui-tree-entry:eq(1)>.layui-tree-main>.layui-tree-txt').trigger('click');
                        return;
                    }*/
                    $('#organizationTree').find('.ew-tree-click').removeClass('ew-tree-click');
                    $(obj.elem).children('.layui-tree-entry').addClass('ew-tree-click');
                    TeacherTable.reload(TeacherTable.renderQuery(selObj));
                }
            });
            $('#organizationTree').find('.layui-tree-entry:eq(0)>.layui-tree-main>.layui-tree-txt').trigger('click');
        }).start();
    }
    renderTree();
    var Organization = {

    };


    /* 点击新增对话框 */
    Organization.openAddDlg = function () {
        func.open({
            title: '添加学校',
            content: Feng.ctxPath + '/view/kindergarten/add',
            tableId: TeacherTable.tableId,
            endCallback: function () {
                renderTree();
            }
        });
    };

    /* 点击编辑对话框 */
    Organization.openEditDlg = function (data) {
        if (selObj.data.id.toString().length==12) {
            Feng.error("请选择一个幼儿园!");
            return ;
        }
        func.open({
            title: '修改学校',
            content: Feng.ctxPath + '/view/kindergarten/edit?orgId=' + data.id,
            tableId: TeacherTable.tableId,
            endCallback: function () {
                renderTree();
            }
        });
    };

    /* 点击删除 */
    Organization.delete = function (data) {
        if (selObj.data.id.toString().length==12) {
            Feng.error("请选择一个幼儿园!");
            return ;
        }
        var operation = function () {
            var httpRequest = new HttpRequest(Feng.ctxPath + "/hrOrganization/delete", 'post', function (data) {
                Feng.success("删除成功!");
                table.reload(TeacherTable.tableId);
                //刷新树
                renderTree();
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            httpRequest.set(data);
            httpRequest.start(true);
        };
        Feng.confirm("是否删除?", operation);
    };

    /* 添加 */
    $('#organizationAddBtn').click(function () {
        Organization.openAddDlg();
    });



    /* 修改 */
    $('#organizationEditBtn').click(function () {
        if (!selObj) return layer.msg('未选择机构', {icon: 2});
        Organization.openEditDlg(selObj.data)
    });

    /* 删除 */
    $('#organizationDelBtn').click(function () {
        if (!selObj) return layer.msg('未选择机构', {icon: 2});
        selObj.data.orgId = selObj.data.id;
        Organization.delete(selObj.data)

    });


});
